package com.gitee.linmt.properties;

import com.gitee.linmt.base.BaseDictionaryEnum;
import com.gitee.linmt.config.DictionaryEnumAutoConfig;
import com.gitee.linmt.enums.DuplicatedBehaviour;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.lang.NonNull;

import java.util.Objects;

/**
 * @author lin-mt
 * @version 1.0.0
 * @since 1.0.0
 */
@ConstructorBinding
@ConfigurationProperties(prefix = DictionaryEnumAutoConfig.MYBATIS_DICTIONARY_ENUM_PREFIX)
public class DictionaryEnumProperties {

    /**
     * 扫描的包地址
     */
    private String enumPackages;

    /**
     * 数据字典枚举的基础类
     */
    private Class<? extends BaseDictionaryEnum> baseEnumClass;

    /**
     * 出现重复code时的告知方式
     */
    private DuplicatedBehaviour duplicated = DuplicatedBehaviour.EXCEPTION;

    /**
     * 是否开启Code层级校验
     */
    private Boolean enableCodeCheck = Boolean.TRUE;

    /**
     * code一层的长度
     */
    private int oneLevelLength = 2;

    /**
     * code最大的层数
     */
    private int maxLevelCount = 3;

    public DictionaryEnumProperties(String enumPackages, Class<? extends BaseDictionaryEnum> baseEnumClass,
                                    DuplicatedBehaviour duplicated, Boolean enableCodeCheck,
                                    Integer oneLevelLength, Integer maxLevelCount) {
        this.enumPackages = enumPackages;
        this.baseEnumClass = baseEnumClass;
        if (Objects.isNull(this.enumPackages) && Objects.isNull(this.baseEnumClass)) {
            throw new IllegalArgumentException("enumPackages 和 baseEnumClass 不能同时为 null");
        }
        if (Objects.nonNull(duplicated)) {
            this.duplicated = duplicated;
        }
        if (Objects.nonNull(enableCodeCheck)) {
            this.enableCodeCheck = enableCodeCheck;
        }
        if (this.enableCodeCheck) {
            if (Objects.nonNull(oneLevelLength)) {
                this.oneLevelLength = oneLevelLength;
                if (this.oneLevelLength <= 0) {
                    throw new IllegalArgumentException("code每层的长度不能小于0");
                }
            }
            if (Objects.nonNull(maxLevelCount)) {
                this.maxLevelCount = maxLevelCount;
                if (this.maxLevelCount <= 0) {
                    throw new IllegalArgumentException("code最大层数不能小于0");
                }
            }
        }
    }

    public String getEnumPackages() {
        return enumPackages;
    }

    public Class<? extends BaseDictionaryEnum> getBaseEnumClass() {
        return baseEnumClass;
    }

    @NonNull
    public DuplicatedBehaviour getDuplicated() {
        return duplicated;
    }

    @NonNull
    public Boolean getEnableCodeCheck() {
        return enableCodeCheck;
    }

    public int getOneLevelLength() {
        return oneLevelLength;
    }

    public int getMaxLevelCount() {
        return maxLevelCount;
    }
}
